package com.activiti.variable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * 流程变量测试
 * 
 * @author zhaoqx
 * 
 */
public class VariableTest {
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void test1() {
		DeploymentBuilder deploymentBuilder = processEngine
				.getRepositoryService().createDeployment();
		deploymentBuilder
				.addClasspathResource("com/itheima/activiti/variable/bxlc.bpmn");
		deploymentBuilder
				.addClasspathResource("com/itheima/activiti/variable/bxlc.png");
		deploymentBuilder.deploy();
	}

	/**
	 * 启动流程实例时设置流程变量
	 */
	@Test
	public void test2() {
		String processDefinitionKey = "bxlc";// 流程定义key
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("key1", "value1");
		variables.put("loginUser", "小王");
		ProcessInstance pi = processEngine.getRuntimeService()
				.startProcessInstanceByKey(processDefinitionKey, variables);
		System.out.println(pi.getId());
	}
	
	/**
	 * 办理任务时设置流程变量
	 */
	@Test
	public void test3() {
		String taskId = "206";//任务id
		Map<String, Object> variables = new HashMap<>();
		variables.put("qjyy", "不想上班");
		variables.put("qjts", 365);
		processEngine.getTaskService().complete(taskId, variables);
	}
	
	/**
	 * 使用RuntimeService的set方法设置流程变量
	 */
	@Test
	public void test4() {
		String executionId = "201";//流程实例id
		Map<String, Object> variables = new HashMap<>();
		variables.put("审批意见", "以后注意");
		variables.put("user", new User(10,20,"北京"));
		processEngine.getRuntimeService().setVariables(executionId, variables);
		//processEngine.getRuntimeService().setVariable(executionId, variableName, value);
	}
	
	/**
	 * 使用TaskService的set方法设置流程变量
	 */
	@Test
	public void test5() {
		String taskId = "304";
		String key = "审批意见";
		Object value = "不批";
		processEngine.getTaskService().setVariable(taskId , key, value);
	}
	
	
	/////////////////////////////////////////////////////////////////
	/**
	 * 使用RuntimeService的get方法设置流程变量
	 */
	@Test
	public void test6() {
		String executionId = "201";
		Map<String, Object> variables = processEngine.getRuntimeService().getVariables(executionId);
		Set<String> keySet = variables.keySet();
		for (String key : keySet) {
			Object value = variables.get(key);
			System.out.println(key + " = " + value);
		}
	}
	
	/**
	 * 使用TaskService的get方法设置流程变量
	 */
	@Test
	public void test7() {
		String taskId = "304";
		Object value = processEngine.getTaskService().getVariable(taskId , "user");
		System.out.println(value);
	}
}
